package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Category;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/category")
@RestController
@Api(tags = "分类管理模块")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 添加分类
     * @param category
     * @return
     */
    @PostMapping
    @ApiOperation(value = "添加分类")
    public R add(@RequestBody Category category){

        //1. 调用service
        int row = categoryService.add(category);

        //2. 响应结果
        if(row > 0){
            return R.success("添加分类成功！");
        }
        return R.error("添加分类失败！");
    }

    /**
     * 分类分页展示
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分类分页展示")
    public R page(PageParam pageParam){

        //1. 调用service
        IPage<Category> page = categoryService.page(pageParam);

        //2. 响应结果
        return R.success(page);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    //使用swagger
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id",value = "删除分类的id",required = true)
            }
    )
    public R delete(Long id){

        //1. 调用service
        int row = categoryService.delete(id);

        //2. 响应结果
        if(row > 0){
            return R.success("删除分类成功！");
        }

        return R.error("删除分类失败！");

    }

    /**
     * 更新分类
     * @param category
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category){

        //1. 调用service
        int row = categoryService.update(category);

        //2. 响应结果
        if(row > 0){
            return R.success("更新分类成功！");
        }
        return R.error("更新分类失败！");
    }


    /**
     * 根据类型值来查找分类数据
     * @param type 1:查询菜品分类 ，2: 查询套餐分类
     * @return
     */
    @GetMapping("/list")
    public R findByType(Integer type){

        //1. 调用service
        List<Category> list = categoryService.findByType(type);

        //2. 响应结果
        return R.success(list);
    }



}
